# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import copy
g_colorDict={'Default'                  :'#000000',
                        'Keyword'                  :'#B0171F'   ,        
                        'KeywordSet2'            :'#7f0000'   ,
                        'CommentDoc'              :'#008000'    , 
                        'Comment'                  :'#008000'    , 
                        'CommentLine'            :'#008000'    ,
                        'Number'                    :'#007f7f' ,
                        'DoubleQuotedString' :'#ff00ff'   ,
                        'SingleQuotedString' :'#ff00ff'  ,
                        'PreProcessor'           :'#be07ff'  ,   
                        'Operator'                 :'#191970',
                        'UnclosedString'       :'#0000FF', 
                        'Background'              :'#b2b2b2', 
                        'CaretLine'              :'#FFCFCF'  
                    }
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(312, 324)
        Dialog.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        Dialog.setAcceptDrops(False)
        Dialog.setSizeGripEnabled(True)
        Dialog.setModal(False)
        self.listStyles = QtWidgets.QListWidget(Dialog)
        self.listStyles.setGeometry(QtCore.QRect(10, 40, 181, 271))
        self.listStyles.setFrameShadow(QtWidgets.QFrame.Plain)
        self.listStyles.setObjectName("listStyles")
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        item = QtWidgets.QListWidgetItem()
        self.listStyles.addItem(item)
        self.defaultButton = QtWidgets.QPushButton(Dialog)
        self.defaultButton.setGeometry(QtCore.QRect(220, 140, 75, 23))
        self.defaultButton.setMaximumSize(QtCore.QSize(16777215, 16777215))
        self.defaultButton.setObjectName("defaultButton")
        self.okButton = QtWidgets.QPushButton(Dialog)
        self.okButton.setGeometry(QtCore.QRect(220, 210, 75, 23))
        self.okButton.setMaximumSize(QtCore.QSize(16777215, 16777215))
        self.okButton.setObjectName("okButton")
        self.cancelButton = QtWidgets.QPushButton(Dialog)
        self.cancelButton.setGeometry(QtCore.QRect(220, 280, 75, 23))
        self.cancelButton.setObjectName("cancelButton")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(10, 10, 171, 21))
        self.label.setObjectName("label")

        self.retranslateUi(Dialog)
        self.okButton.clicked.connect(Dialog.accept)    #Dialog.accept() 是内建函数，不用使用者实现
        self.cancelButton.clicked.connect(Dialog.reject)    #Dialog.reject() 是内建函数，不用使用者实现
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "color Dialog"))
        __sortingEnabled = self.listStyles.isSortingEnabled()
        self.listStyles.setSortingEnabled(False)
        item = self.listStyles.item(0)
        item.setText(_translate("Dialog", "Default"))
        item = self.listStyles.item(1)
        item.setText(_translate("Dialog", "Keyword"))
        item = self.listStyles.item(2)
        item.setText(_translate("Dialog", "KeywordSet2"))
        item = self.listStyles.item(3)
        item.setText(_translate("Dialog", "CommentDoc"))
        item = self.listStyles.item(4)
        item.setText(_translate("Dialog", "Comment"))
        item = self.listStyles.item(5)
        item.setText(_translate("Dialog", "CommentLine"))
        item = self.listStyles.item(6)
        item.setText(_translate("Dialog", "Number"))
        item = self.listStyles.item(7)
        item.setText(_translate("Dialog", "DoubleQuotedString"))
        item = self.listStyles.item(8)
        item.setText(_translate("Dialog", "SingleQuotedString"))
        item = self.listStyles.item(9)
        item.setText(_translate("Dialog", "PreProcessor"))
        item = self.listStyles.item(10)
        item.setText(_translate("Dialog", "Operator"))
        item = self.listStyles.item(11)
        item.setText(_translate("Dialog", "UnclosedString"))
        item = self.listStyles.item(12)
        item.setText(_translate("Dialog", "Background"))
        item = self.listStyles.item(13)
        item.setText(_translate("Dialog", "CaretLine"))
        self.listStyles.setSortingEnabled(__sortingEnabled)
        self.defaultButton.setText(_translate("Dialog", "默认"))
        self.okButton.setText(_translate("Dialog", "OK"))
        self.cancelButton.setText(_translate("Dialog", "CANCEL"))
        self.label.setText(_translate("Dialog", "点击相应文字，即可修改颜色"))

class   ColorDlg(QDialog,Ui_Dialog):
    def __init__(self, parent=None):
        super(ColorDlg, self).__init__(parent)
        self.setupUi(self)
        if parent == None:
            return
        #1.主窗口作为父窗口 ，颜色的初始值由主窗口传入
        self.parent = parent  
        self.DlgRepaint()
    def DlgRepaint(self):
        #2.设置每个item的颜色
        for i in range(0, self.listStyles.count()):
            itm = self.listStyles.item(i)
            cl=QColor(self.parent.colorDict[itm.text()])
            #2.1 编辑区的背景 和 焦点行背景，设置底色
            if itm.text() == 'Background' or itm.text() == 'CaretLine':
                itm. setBackground(cl)
            #2.2 其它项设置字体颜色
            else:
                itm.setForeground(cl)            
        self.repaint()
        #3. 点击查找按钮的信号的槽函数
    def on_listStyles_clicked(self):
        itm=self.listStyles.currentItem()
        cl=QColorDialog.getColor(QColor(self.parent.colorDict[itm.text()]))
        if cl.isValid():
            try:
                self.parent.colorDict[itm.text()] = cl.name()   #把有效的颜色保存到颜色字典中
            except:
                pass
            # 把刚刚点击的地方更新颜色
            if itm.text() == 'Background' or itm.text() == 'CaretLine':
                itm. setBackground(cl)
            else:
                itm.setForeground(cl)
            #self.repaint()
    def on_defaultButton_clicked(self):
        global g_colorDict
        self.parent.colorDict=copy.deepcopy(g_colorDict)  
        self.DlgRepaint()
'''            
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    #Dialog = QtWidgets.QDialog()
    ui = ColorDlg()
    #ui.setupUi(Dialog)
    ui.show()
    sys.exit(app.exec_())
'''
